import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/layout/index.vue'),
      children: [
        {
          path: '/home',
          meta: {
            title: '首页'
          },
          component: () => import('@/views/home/index.vue')
        },
        {
          path: '/my',
          meta: {
            title: '我的'
          },
          component: () => import('@/views/my/index.vue')
        }
      ]
    },
    {
      path: '/adopt',
      meta: {
        title: '被采纳建议页'
      },
      component: () => import('@/views/adopt/index.vue')
    },
    {
      path: '/mywallet',
      meta: {
        title: '我的钱包'
      },
      component: () => import('@/views/mywallet/index.vue')
    },
    {
      path: '/patientsearch',
      meta: {
        title: '病友圈搜索'
      },
      component: () => import('@/views/patientsearch/index.vue')
    },
    {
      path: '/improveone',
      meta: {
        title: '完善信息1'
      },
      component: () => import('@/views/improveone/index.vue')
    },
    {
      path: '/login',
      meta: {
        title: '登录',
        isLogin: true
      },
      component: () => import('@/views/login/index.vue')
    },
    {
      path: '/start',
      meta: {
        title: '启动页'
      },
      component: () => import('@/views/start/index.vue')
    },
    {
      path: '/register',
      meta: {
        title: '注册',
        isLogin: true
      },
      component: () => import('@/views/register/index.vue')
    },
    {
      path: '/bindcard',
      meta: {
        title: '绑定卡片'
      },
      component: () => import('@/views/bindcard/index.vue')
    },
    {
      path: '/verifyidcard',
      meta: {
        title: '验证身份证'
      },
      component: () => import('@/views/verifyidcard/index.vue')
    },
    {
      path: '/bindidcard',
      meta: {
        title: '绑定卡片(身份证)'
      },
      component: () => import('@/views/bindidcard/index.vue')
    },
    {
      path: '/verifybankcard',
      meta: {
        title: '绑定银行卡'
      },
      component: () => import('@/views/verifybankcard/index.vue')
    },
    {
      path: '/bindcardbankcard',
      meta: {
        title: '绑定卡片(银行卡)'
      },
      component: () => import('@/views/bindcardbankcard/index.vue')
    },
    {
      path: '/withdrawalpage',
      meta: {
        title: '提现页'
      },
      component: () => import('@/views/withdrawalpage/index.vue')
    },
    {
      path: '/withdrawingprogress',
      meta: {
        title: '提现中'
      },
      component: () => import('@/views/withdrawingprogress/index.vue')
    },
    {
      path: '/withdrawalrecord',
      meta: {
        title: '提现记录'
      },
      component: () => import('@/views/withdrawalrecord/index.vue')
    },
    {
      path: '/patientearchresults',
      meta: {
        title: '病友圈搜索结果页'
      },
      component: () => import('@/views/patientearchresults/index.vue')
    },
    {
      path: '/mymessage',
      meta: {
        title: '我的消息'
      },
      component: () => import('@/views/mymessage/index.vue')
    },
    {
      path: '/receiptmessage',
      meta: {
        title: 'H币入账消息'
      },
      component: () => import('@/views/receiptmessage/index.vue')
    },
    {
      path: '/systemmessage',
      meta: {
        title: '系统消息'
      },
      component: () => import('@/views/systemmessage/index.vue')
    },
    {
      path: '/consultationmessage',
      meta: {
        title: '问诊消息'
      },
      component: () => import('@/views/consultationmessage/index.vue')
    },
    {
      path: '/myprofile',
      meta: {
        title: '我的资料'
      },
      component: () => import('@/views/myprofile/index.vue')
    },
    {
      path: '/setportraitphoto',
      meta: {
        title: '设置形象照'
      },
      component: () => import('@/views/setportraitphoto/index.vue')
    },
    {
      path: '/customimagephoto',
      meta: {
        title: '自定义形象照'
      },
      component: () => import('@/views/customimagephoto/index.vue')
    },
    {
      path: '/replaceimagephoto',
      meta: {
        title: '更换形象照'
      },
      component: () => import('@/views/replaceimagephoto/index.vue')
    },
    {
      path: '/selectimagephoto',
      meta: {
        title: '选择形象照方式'
      },
      component: () => import('@/views/selectimagephoto/index.vue')
    },
    {
      path: '/setautomaticreply',
      meta: {
        title: '设置自动回复'
      },
      component: () => import('@/views/setautomaticreply/index.vue')
    },
    {
      path: '/consultationlist',
      meta: {
        title: '咨询列表'
      },
      component: () => import('@/views/consultationlist/index.vue')
    },
    {
      path: '/dialoguechat',
      meta: {
        title: '对话聊天页'
      },
      component: () => import('@/views/dialoguechat/index.vue')
    },
    {
      path: '/historicalconsultation',
      meta: {
        title: '历史问诊'
      },
      component: () => import('@/views/historicalconsultation/index.vue')
    },
    {
      path: '/viewreviews',
      meta: {
        title: '查看评价'
      },
      component: () => import('@/views/viewreviews/index.vue')
    },
    // {
    //   path: '/parent',
    //   meta: {
    //     title: '父组件'
    //   },
    //   component: () => import('@/views/parent/index.vue')
    // },
    // {
    //   path: '/children',
    //   meta: {
    //     title: '自组件'
    //   },
    //   component: () => import('@/views/children/index.vue')
    // },
    {
      path: '/consultationchatrecords',
      meta: {
        title: '问诊聊天记录'
      },
      component: () => import('@/views/consultationchatrecords/index.vue')
    },
    {
      path: '/giftlist',
      meta: {
        title: '礼物列表'
      },
      component: () => import('@/views/giftlist/index.vue')
    },
    {
      path: '/patientInformation',
      meta: {
        title: '患者信息页'
      },
      component: () => import('@/views/patientInformation/index.vue')
    },
    {
      path: '/improvetwo',
      meta: {
        title: '完善信息2'
      },
      component: () => import('@/views/improvetwo/index.vue')
    },
    {
      path: '/improvethree',
      meta: {
        title: '完善信息3'
      },
      component: () => import('@/views/improvethree/index.vue')
    },
    {
      path: '/checkin',
      meta: {
        title: '申请入住',
        isLogin: true
      },
      component: () => import('@/views/checkin/index.vue')
    },
    {
      path: '/forgetone',
      meta: {
        title: '忘记密码1',
        isLogin: true
      },
      component: () => import('@/views/forgetone/index.vue')
    },
    {
      path: '/forgettwo',
      meta: {
        title: '忘记密码2',
        isLogin: true
      },
      component: () => import('@/views/forgettwo/index.vue')
    },
    {
      path: '/patienthome',
      meta: {
        title: '病友圈首页'
      },
      component: () => import('@/views/patienthome/index.vue')
    },
    {
      path: '/noadoption/:id',
      meta: {
        title: '无采纳建议'
      },
      component: () => import('@/views/noadoption/index.vue')
    },
    {
      path: '/writesuggestions',
      meta: {
        title: '写建议页'
      },
      component: () => import('@/views/writesuggestions/index.vue')
    },
    {
      path: '/answer',
      meta: {
        title: '解答成功页'
      },
      component: () => import('@/views/answer/index.vue')
    },
    {
      path: '/login',
      meta: {
        title: '登录',
        isLogin: true
      },
      component: () => import('@/views/login/index.vue')
    },
    {
      path: '/start',
      meta: {
        title: '启动页'
      },
      component: () => import('@/views/start/index.vue')
    },
    {
      path: '/register',
      meta: {
        title: '注册'
      },
      component: () => import('@/views/register/index.vue')
    },
    {
      path: '/abc',
      meta: {
        title: 'abc'
      },
      component: () => import('@/views/abc/index.vue')
    }
  ]
})

// // 路由守卫
router.beforeEach((to, from, next) => {
  // 获取Token
  const token = localStorage.getItem('result') // 如果没有Token，则跳转到登录页
  if (to.meta.isLogin) {
    next()
  } else {
    if (!token && to.path !== '/login') {
      next('/login')
    } else {
      next()
    }
  }
})

export default router
